<?php
require_once ('config.php');
require_once ('databasefunctions.php');
require_once ('displayfunctions.php');
require_once ('businessfunctions.php');
validate_user();
html_header(); ?>

<script>
function opensearch(search_type, fieldname) {
    url_text = 'search.php?master=' + search_type + '&inputname=' + fieldname;
    window.open(url_text ,'','width=700,height=500');
}
</script>

<?php
//Show menu

switch ($_GET['action']) {
	case "add_form" :

		title_bar($langprojectmaster);
		?>
		<br><? echo $langoperation . " - " . $langadd ?><br>
		<form method = "GET" action="projectmaster.php" name="project">
		<table>
			<tr>
				<td bgcolor='<? echo $color1 ?>'><? echo $langclientid ?>
                <td><input type = "text" name ="ClientID" size = "10" maxlength="10" <?
                if ($sql_data['ClientID']) echo "value ='" . $sql_data['ClientID'] . "'";
                else if ($_SESSION['saved']['ClientID']) echo "value ='" . $_SESSION['saved']['ClientID'] . "'";
                ?> onBlur="ToUpperCase(document.transaction.ClientID)">
                <input type = 'button' value = 'S' onclick=opensearch('client','project.ClientID')>
			<tr>
				<td bgcolor='<? echo $color1 ?>'><? echo $langprojectid ?>
				<td><input type = 'text' name = 'ProjectID' maxlength = '20'>
			<tr>
				<td bgcolor='<? echo $color1 ?>'><? echo $langprojectname ?>
				<td><input type = 'text' name = 'ProjectName' maxlength = '50'>
			<tr>
				<td bgcolor='<? echo $color1 ?>'><? echo $langenablestatus ?>
				<td><select name = 'Enable'>
						<option value='0'><? echo $langdisable ?>
						<option value='1'><? echo $langenable ?>
					</select>

		</table>
		<input type = 'hidden' name = 'action' value = 'add'>
		<input type = 'submit' value = '<? echo $langadd ?>'>
		</form>
		<?
		break;

	case "add" :
		if (empty($_GET['ClientID']) || empty($_GET['ProjectID'])) {
			message_box($langclientid . ", " . $langprojectid . " " . $langcannotempty);
			?>
				<script>history.go(-1);</script>
			<?
			exit;
		}
		$sql = "INSERT INTO projectmaster (ClientID, ProjectID, ProjectName, Enable) values (";
		$sql .= "'" . strtoupper($_GET['ClientID']) . "', ";
		$sql .= "'" . strtoupper($_GET['ProjectID']) . "', ";
		$sql .= "'" . strtoupper($_GET['ProjectName']) . "', ";
		$sql .= "'" . $_GET['Enable'] . "'";
		$sql .= ")";

		$sql_result = sql_execute($sql);
		echo "<br>$langprojectid $langadded<br>";
		title_bar($langprojectmaster,1);
		break;

	case "modify_form" :

		title_bar($langprojectmaster, 1);
		// Obtain values from table
		$sql = "SELECT * FROM projectmaster WHERE ProjectID='" . $_GET['ProjectID']. "'";
		$sql_result = sql_execute($sql);
		$sql_data = mysql_fetch_array($sql_result);

		?>
		<br><? echo $langoperation . " - " . $langmodify ?><br>
		<form method = "GET" action="projectmaster.php">
		<table>
			<tr>
				<td bgcolor='<? echo $color1 ?>'><? echo $langclientid ?>
				<td><input type = 'text' name = 'ClientID' value = '<? echo $_GET['ClientID'] ?>'
					maxlength = '10' readonly>
			<tr>
				<td bgcolor='<? echo $color1 ?>'><? echo $langprojectid ?>
				<td><input type = 'text' name = 'ProjectID' maxlength = '20' value='<?
						echo $sql_data['ProjectID'] ?> ' readonly>
			<tr>
				<td bgcolor='<? echo $color1 ?>'><? echo $langprojectname ?> 1
				<td><input type = 'text' name = 'ProjectName' maxlength = '100' value='<?
						echo $sql_data['ProjectName'] ?> '>
			<tr>
				<td bgcolor='<? echo $color1 ?>'><? echo $langenablestatus ?>
				<td><select name = 'Enable'>
					<?
					if ($sql_data['Enable'] == '0') {
						echo "<option value='0' SELECTED>$langdisable";
						echo "<option value='1'>$langenable";
					}
					else {
						echo "<option value='0'>$langdisable";
						echo "<option value='1' SELECTED>$langenable";
					}
					?>
					</select>
		</table>
		<input type = 'hidden' name = 'action' value = 'modify'>
		<input type = 'submit' value = '<? echo $langmodify ?>'>
		</form>
		<?
		break;

	case "modify" :
		$sql = "UPDATE projectmaster SET ";
		$sql .= "ProjectName = '" . $_GET['ProjectName'] . "', ";
		$sql .= "Enable = '" . $_GET['Enable'] . "' ";
		$sql .= "WHERE ClientID = '" . $_GET['ClientID'] . "' ";
		$sql .= "AND   ProjectID = '" . $_GET['ProjectID'] . "'";

		$sql_result = sql_execute($sql);
		echo "<br>$langprojectid $langmodified<br>";
		title_bar($langprojectmaster, 1);
		break;

	case "delete" :
		$sql = "SELECT ClientID, ProjectID FROM billtransaction"
		." WHERE ClientID = '" . $_GET['ClientID'] ."'"
		." AND   ProjectID ='" . $_GET['ProjectID'] ."'"
		." LIMIT 1";
		$sql_result = sql_execute($sql);
		$sql_data = mysql_fetch_array($sql_result);
		if ($sql_data[0]) {
			$message = $langtransactionexist . "<br>" . $langcannotdelprj . "<br>";
			message_box($message);
			?>
				<script>history.go(-1);</script>
			<?
			exit;
		}

		$sql = "DELETE FROM projectmaster WHERE ClientID = '" . $_GET['ClientID'] . "' AND ProjectID=" . $_GET['ProjectID'] . "'";

		$sql_result = sql_execute($sql);
		echo "<br>$langprojectid $langdeleted<br>";
		title_bar($langprojectmaster, 1);
		break;

	case "count" :
		save_search_list();
		$url_text = retrieve_search_list();
		$sql = select_statement('projectmaster');
		$sql_result = sql_execute($sql);
		$sql_data = mysql_fetch_array($sql_result);
		$no_of_record = $sql_data[0];

		?>
		<script>
		this.location = 'projectmaster.php?action=browse&total_record=<? echo $no_of_record ?>
						&record_offset=0<? echo $url_text ?>'
		</script>
		<?
		break;

	case "browse" :
		navigation_bar($langprojectmaster, 1);
		column_headings(array($langclientid, $langprojectid, $langprojectname, $langenablestatus));

		// List projects here
		$sql = select_statement('projectmaster',1);
		$sql.= " ORDER BY ClientID,ProjectID LIMIT " . $_GET['record_offset'] . ", " . $_SESSION['RecordPerPage'];
		$sql_result = sql_execute($sql);

		while ($sql_data = mysql_fetch_array($sql_result)) {
			$rowcolor = row_color_change();
			echo "\n<tr bgcolor='$rowcolor'>";
			echo "<td>";
			echo "<a href = 'projectmaster.php?action=modify_form&ClientID=".$sql_data['ClientID']."&ProjectID=".$sql_data['ProjectID'];
			echo "'>".$sql_data['ClientID']."</a>";
			echo "<td>";
			echo $sql_data['ProjectID'];
			echo "<td>";
			echo $sql_data['ProjectName'];
			echo "<td>";
			if ($sql_data['Enable'] == 1) echo $langyes;
			else echo $langno;
			echo "<td>";
			echo "<a href = 'projectmaster.php?action=delete&ClientID=" .
					$sql_data['ClientID']."&ProjectID=".$sql_data['ProjectID']."'>$langdelete</a>";
		}
		?>
		</table>
		<?
		break;

	default :
		title_bar($langprojectmaster, 1);
		$field[] = array('title'=>$langclientid, 'field'=>'ClientID', 'length'=>10);
		$field[] = array('title'=>$langprojectid, 'field'=>'ProjectID', 'length'=>20);
		search_form($field, $_SERVER['PHP_SELF']);
		break;
}

html_footer();
?>